﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace PBOffice.DAL
{
    /// <summary>
    /// 执行sql的通用的方法
    /// </summary>
    public static class SqlHelp
    {

        //数据库连接字符串ConfigurationManager.ConnectionStrings["Connection"].ToString();
       // Data Source=.;Initial Catalog=MyBlog;User ID=sa;Password=feng
        public static readonly string connectionstring = "Data Source=.;Initial Catalog=OA;User ID=sa;Password=feng";

        /// <summary>
        /// 设置一个等待执行存储过程的SqlCommand对象
        /// </summary>
        /// <param name="cmd">SqlCommand 对象，不允许空对象</param>
        /// <param name="conn">SqlConnection 对象，不允许空对象</param>
        /// <param name="spName">Sql 语句</param>
        /// <param name="parameterValues">不定个数的存储过程参数，允许为空</param>
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, string spName, params object[] parameterValues)
        {
            //打开连接
            if (conn.State != ConnectionState.Open)
                conn.Open();

            //设置SqlCommand对象
            cmd.Connection = conn;
            cmd.CommandText = spName;
            cmd.CommandType = CommandType.StoredProcedure;

            //获取存储过程的参数
            SqlCommandBuilder.DeriveParameters(cmd);

            //移除Return_Value 参数
            cmd.Parameters.RemoveAt(0);

            //设置参数值
            if (parameterValues != null)
            {
                for (int i = 0; i < cmd.Parameters.Count; i++)
                {
                    cmd.Parameters[i].Value = ((SqlParameter)parameterValues[i]).Value;

                }
            }
        }
        /// <summary>
        /// 设置一个等待执行的SqlCommand对象
        /// </summary>
        /// <param name="cmd">SqlCommand 对象，不允许空对象</param>
        /// <param name="conn">SqlConnection 对象，不允许空对象</param>
        /// <param name="commandText">Sql 语句</param>
        /// <param name="cmdParms">SqlParameters  对象,允许为空对象</param>
        private static void PrepareCommand(SqlCommand cmd, CommandType commandType, SqlConnection conn, string commandText, SqlParameter[] cmdParms)
        {
            //打开连接
            if (conn.State != ConnectionState.Open)
                conn.Open();

            //设置SqlCommand对象
            cmd.Connection = conn;
            cmd.CommandText = commandText;
            cmd.CommandType = commandType;

            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }

        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="connectionstring">连接字符串</param>
        /// <param name="commandType">命令类型</param>
        /// <param name="commandText">命令文本</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string connectionstring, CommandType commandtype, string commandText, params SqlParameter[] commandParameters)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                PrepareCommand(cmd, commandtype, conn, commandText, commandParameters);
                int val = cmd.ExecuteNonQuery();

                return val;
            }

        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="connectionstring">连接字符串</param>
        /// <param name="spName">存储过程名</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteNonQuery(string connectionstring, string spName, params SqlParameter[] commandParameters)
        {
            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                SqlCommand cmd = new SqlCommand();
                PrepareCommand(cmd, conn, spName, commandParameters);
                int val = cmd.ExecuteNonQuery();
                return val;
            }
        }
        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="connectionstring">连接字符串</param>
        /// <param name="commandtype">命令类型</param>
        /// <param name="commandtext">命令文本</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns>DateReader对象</returns>
        public static SqlDataReader ExecuteReader(string connectionstring, CommandType commandtype, string commandtext, params SqlParameter[] commandParameters)
        {
            SqlConnection conn = new SqlConnection(connectionstring);
            try
            {
                SqlCommand cmd = new SqlCommand();
                PrepareCommand(cmd, commandtype, conn, commandtext, commandParameters);
                SqlDataReader data = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return data;
            }
            catch
            {
                conn.Close();
                throw;
            }

        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="connectionstring">连接字符串</param>
        /// <param name="spname">存储过程名</param>
        /// <param name="commandParameters">参数数组</param>
        /// <returns>DataReader对象</returns>
        public static SqlDataReader ExecuteReader(string connectionstring, string spname, params SqlParameter[] commandParameters)
        {
            SqlConnection conn = new SqlConnection(connectionstring);
            try
            {
                SqlCommand cmd = new SqlCommand();

                PrepareCommand(cmd, conn, spname, commandParameters);
                SqlDataReader data = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return data;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="connectionstring">连接字符串</param>
        /// <param name="commandtype">命令类型</param>
        /// <param name="commandtext">命令文本</param>
        /// <param name="commandparameter">参数数组</param>
        /// <returns>DataSet对象</returns>
        public static DataSet ExecuteDataSet(string connectionstring, CommandType commandtype, string commandtext, params SqlParameter[] commandparameter)
        {
            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                SqlCommand cmd = new SqlCommand();
                PrepareCommand(cmd, commandtype, conn, commandtext, commandparameter);
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    return ds;

                }

            }

        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="connectionstring">连接字符串</param>
        /// <param name="spname">存储过程名</param>
        /// <param name="commandparameters">参数数组</param>
        /// <returns>DataSet对象</returns>
        public static DataSet ExecuteDataSet(string connectionstring, string spname, params SqlParameter[] commandparameters)
        {
            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                SqlCommand cmd = new SqlCommand();
                PrepareCommand(cmd, conn, spname, commandparameters);
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    return ds;
                }


            }
        }
        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="connectionstring">连接字符串</param>
        /// <param name="commandtype">命令类型</param>
        /// <param name="commandtext">命令文本</param>
        /// <param name="commandparameters">参数数组</param>
        /// <returns></returns>
        public static object ExecuteScalar(string connectionstring, CommandType commandtype, string commandtext, params SqlParameter[] commandparameters)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                PrepareCommand(cmd, commandtype, conn, commandtext, commandparameters);
                object val = cmd.ExecuteScalar();
                return val;
            }
        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="connectionstring">连接字符串</param>
        /// <param name="spname">存储过程名</param>
        /// <param name="commandparameters">参数数组</param>
        /// <returns></returns>
        public static object ExecuteScalar(string connectionstring, string spname, params object[] commandparameters)
        {
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                PrepareCommand(cmd, conn, spname, commandparameters);
                object val = cmd.ExecuteScalar();
                return val;
            }
        }

    }
}
